﻿package com.saint.utils{
	
	public class TimeUtils{
		
		private static var d:Date;
		
		// return ranges
		public static const SILENT:uint = 0;
		public static const SECONDS:uint = 1;
		public static const MINUTES:uint = 2;
		public static const HOURS:uint = 3;
		
		public function TimeUtils(){
		}
		
		/**
		 * returns date in the format: YYYY-MM-DD HH:MM:SS
		 * @return
		 */
		public static function formatedDateTime():String{
			var dateStr:String = formatedDate() +  " " + formatedTime();
			return dateStr;
		}
		
		/**
		 * returns date in the format: YYYY-MM-DD
		 * @return
		 */
		public static function formatedDate():String{
			d = new Date();
			var dateStr:String = 
				String(d.fullYear) + "-" +
				(d.month < 10 ? "0" : "") + String(d.month) + "-" +
				(d.date < 10 ? "0" : "") + String(d.date);
			return dateStr;
		}
		
		/**
		 * returns time in the format: HH:MM:SS
		 * @return
		 */
		public static function formatedTime():String{
			d = new Date();
			var timeStr:String = 
				(d.hours < 10 ? "0" : "") + String(d.hours) + ":" +
				(d.minutes < 10 ? "0" : "") + String(d.minutes) + ":" +
				(d.seconds < 10 ? "0" : "") + String(d.seconds);
			return timeStr;
		}
		
		/**
		 * returns time in the format: MM:SS
		 * @param	time
		 * @return
		 */
		public static function formatedTimerCount(time:Number, range:uint = MINUTES):String {
			//trace(time)
			var hours:Number = Math.floor(time / 3600);
			var minutes:Number = Math.floor((time / 60) % 60);
			var seconds:Number = Math.floor(time % 60);
			var timeStr:String = 
				(range >= HOURS ? maintainTwoCharacterTimeFormat(hours) + ":" : "") +
				(range >= MINUTES ? maintainTwoCharacterTimeFormat(minutes) + ":" : "") +
				(range >= SECONDS ? maintainTwoCharacterTimeFormat(seconds) : "");
			return timeStr;
		}
		
		private static function maintainTwoCharacterTimeFormat(num:Number):String {
			return (num < 10 ? "0" : "") + String(num);
		}
	}
}